package com.pengtu.gsj.service.impl.check.lzgl.roadProtect;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.lzgl.RoadFileManDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.RoadFileMan;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.roadProtect.RoadAdminFileService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;


@Service
@Transactional
public class RoadAdminFileServiceImpl implements RoadAdminFileService {

	@Autowired
	public SystemManageService systemManageService;
	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public RoadFileManDao roadFileManDao;
	
	@Override
	public Map<String, Object> getRoadAdminFiles(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<Double> scoLists = new ArrayList<Double>();
		List<RoadFileMan> roadFileMans = new ArrayList<RoadFileMan>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		String unitId = (String)jsonObject.get("itemProjectId");
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double tableFull = MathsUtils.sum(yearFull, yearPart * 2);
		double total = 0;
		double tempSco = 0;
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		map.put("att", list);
		//1、查看路政档案管理是否有文件，如果有就添加到评分-省级路政管理档案制度列表中
		list = systemManageService.getCheckAttachmentByTag(startTime, endTime, "lzdagl_glzd");
		if (list.size() > 0) {
			map.put("glzdAtt", list);
			map.put("flag", true);
			map.put("scoreOne", yearFull);
			total += yearFull;
		} else {
			map.put("scoreOne", 0);
		}
		//2、查询每个路政基层执法站是否所档案齐全，登记规范有序，保存期限满足要求，由专人管理的
		if (StringUtils.isNotEmpty(unitId)) {
			map.put("flagTwo", true);
			//通过逗号将每个id切割出来，并搜索算分
			String[] idArrs = unitId.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				double temp = 0;
				//根据tag检索是否有附件
				if( i > 1) {
					break;
				}
				RoadFileMan roadFileMan = roadFileManDao.get(idArrs[i]);
				roadFileMans.add(roadFileMan);
				temp += "1".equals(roadFileMan.getIfFileCom()) ? 1 : 0;
				temp += "1".equals(roadFileMan.getIfRegSpeci()) ? 1 : 0;
				temp += "1".equals(roadFileMan.getIfSaveTlimit()) ? 1 : 0;
				temp += "1".equals(roadFileMan.getIfSpeciMan()) ? 1 : 0;
				if (4 == temp) {
					tempSco = yearPart;
				} else if (3 == temp) {
					tempSco = yearPart / 2;
				} else {
					tempSco = 0;
				}
				scoLists.add(tempSco);
				total += tempSco;
			}
		}
		//根据总得分算出完善度
		double percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("manageData", roadFileMans);
		map.put("scoLists", scoLists);
		map.put("percent", percentage);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
}